package com.zqq.message.receiver

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import cn.jpush.android.api.JPushInterface
import com.alibaba.android.arouter.launcher.ARouter
import com.eightbitlab.rxbus.Bus
import com.zhuanju.core.utils.ZLog
import com.zqq.provider.common.ProviderConstant
import com.zqq.provider.event.MessageBadgeEvent
import com.zqq.provider.router.RouterPath
import org.json.JSONObject

/**
 * 自定义Push 接收器
 * 3.3.0开始是通过继承 JPushMessageReceiver并配置来接收所有事件回调。
 * (请根据文档来，这里是按照3.3.0以前的写法)
 * @author zqq on 2019/9/28.
 */
class MessageReceiver : BroadcastReceiver() {

    override fun onReceive(context: Context?, intent: Intent) {
        val bundle = intent.extras
        ZLog.e("onReceiver:" + intent.action + ", extras: " + bundle)

        when {
            JPushInterface.ACTION_REGISTRATION_ID == intent.action -> {
                ZLog.e("JPush注册成功")
            }
            JPushInterface.ACTION_MESSAGE_RECEIVED == intent.action -> {
                ZLog.e("接受到推送下来的自定义消息")
                Bus.send(MessageBadgeEvent(true))

                //1.发送方，2.接收方需要注册及销毁
                //EventBus.getDefault().post(MessageBadgeEvent(true))
            }
            JPushInterface.ACTION_NOTIFICATION_RECEIVED == intent.action -> {
                ZLog.e("接受到推送下来的通知")
            }
            JPushInterface.ACTION_NOTIFICATION_OPENED == intent.action -> {
                ZLog.e("用户点击打开了通知")
                val extra = bundle.getString(JPushInterface.EXTRA_EXTRA)
                val json = JSONObject(extra)
                val orderId = json.getInt("orderId")

                ARouter.getInstance().build(RouterPath.MessageCenter.PATH_MESSAGE_ORDER)
                        .withInt(ProviderConstant.KEY_ORDER_ID, orderId)
                        .navigation()

            }
            else -> {
                ZLog.e("Unhandled intent - " + intent.action)
            }
        }
    }


}